#!/usr/bin/env python
import webapp2
import urllib2

from google.appengine.api import memcache

# URL to retrieve generated PBI file
# For example, if you generate in cron on Amazon EC2
FETCH_URL="http://example.org/tpp1.pbi"


class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'image/pbi'
        self.response.out.write(self._get())

    def _get(self):
        data = memcache.get("tpp1")
        if data is not None:
            return data
        else:
            data = urllib2.urlopen(FETCH_URL).read()
            memcache.add("tpp1", data, 60)
            return data

app = webapp2.WSGIApplication([
    ('/tpp1.pbi', MainHandler)
], debug=True)
